package central;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import GUI.Ventana;
import control_monitor.Monitor;


public class pieza_A implements Runnable{

	private Monitor monitor; 
	private int socketR1;
	private int socketmaq1;
	private int socketmaq2;
	private int socketR2;
	private int socketR3;
	Ventana gui;
	
	public pieza_A(Monitor monit,int sokr1,int sokr2,int sokr3,int sokm1,int sokm2, Ventana _gui){
		monitor=monit;
		socketR1=sokr1;
		socketmaq1=sokm1;
		socketmaq2=sokm2;
		socketR2=sokr2;
		socketR3=sokr3;
		gui = _gui;
	}
	
	public void run(){	
			//ejecutar el camino A
			monitor.ejecutar_transicion(13);
			gui.SetRobot(1, 4);
			gui.setnumI(1, -1);
					
			DataOutputStream sale_flujoR1;
			Socket socketA = null;
			try {
				socketA=new Socket("localhost",socketR1);
				sale_flujoR1 = new DataOutputStream(socketA.getOutputStream());
				sale_flujoR1.writeUTF("mover pieza de I1 a M1");
				//sale_flujoR1.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			String mensaje = null;
			try {
				DataInputStream entra_flujoR1=new DataInputStream(socketA.getInputStream());
				mensaje = entra_flujoR1.readUTF();
				System.out.println(mensaje);
				//entra_flujoR1.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} //robot confirma movimiento
			
			try {
				socketA.close();
			} catch (IOException e4) {
				// TODO Auto-generated catch block
				e4.printStackTrace();
			}
			
			
			gui.SetRobot(1, 5);
			monitor.ejecutar_transicion(14);
			gui.setMaquina(1, 1, 1);
			
			
			DataOutputStream sale_flujoM1;
			Socket socketB = null;
			try {
				socketB=new Socket("localhost",socketmaq1);
				sale_flujoM1 = new DataOutputStream(socketB.getOutputStream());
				sale_flujoM1.writeUTF("procesar pieza m1");
				//sale_flujoM1.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			DataInputStream entra_flujoM1;
			String mensajemaq=null;
			try {
				entra_flujoM1 = new DataInputStream(socketB.getInputStream());

				mensajemaq=entra_flujoM1.readUTF();//maquina confirma procesamiento

				System.out.println(mensajemaq);
				//entra_flujoM1.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				socketB.close();
			} catch (IOException e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			}
			

			monitor.ejecutar_transicion(15);
			gui.SetRobot(2, 3);
			gui.setMaquina(1, -1, 1);
			
			DataOutputStream sale_flujoR2;
			Socket socketC = null;
			try {
				socketC =new Socket("localhost",socketR2);
				sale_flujoR2 = new DataOutputStream(socketC.getOutputStream());
				sale_flujoR2.writeUTF("mover pieza de m1 a m2");
				//sale_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

			String mensajeR2 = null;
			DataInputStream entra_flujoR2;
			try {
				entra_flujoR2 = new DataInputStream(socketC.getInputStream());
				mensajeR2=entra_flujoR2.readUTF();//robot confirma movimiento
				System.out.println(mensajeR2);
				//entra_flujoR2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				socketC.close();
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			
			gui.SetRobot(2, 5);
			monitor.ejecutar_transicion(16);
			gui.setMaquina(2, 1, 1);
			
			
			DataOutputStream sale_flujoM2;
			Socket socketD = null;
			try {
				socketD =new Socket("localhost",socketmaq2);
				sale_flujoM2 = new DataOutputStream(socketD.getOutputStream());
				sale_flujoM2.writeUTF("procesar pieza en m2");
				//sale_flujoM2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String mensajemaq2 = null;
			
			try {
				DataInputStream entra_flujoM2=new DataInputStream(socketD.getInputStream());
				mensajemaq2=entra_flujoM2.readUTF();//maquina confirma procesamiento
				//entra_flujoM2.close();
				System.out.println(mensajemaq2);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			try {
				socketD.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			

			monitor.ejecutar_transicion(17);
			gui.SetRobot(3, 3);
			gui.setMaquina(2, -1, 1);
			
			
			
			
			DataOutputStream sale_flujoR3;
			Socket socketE = null;
			try {
				socketE =new Socket("localhost",socketR3);
				sale_flujoR3 = new DataOutputStream(socketE.getOutputStream());
				sale_flujoR3.writeUTF("mover pieza de m2 a O1");
				//sale_flujoR3.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String mensajeR3 = null;
			DataInputStream entra_flujoR3;
			try {
				entra_flujoR3 = new DataInputStream(socketE.getInputStream());
				mensajeR3=entra_flujoR3.readUTF();//robot confirma movimiento
				System.out.println(mensajeR3);
				//entra_flujoR3.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				socketE.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			gui.SetRobot(3, 0);
			monitor.ejecutar_transicion(18);
			gui.setnumO(1, 1);
			
			System.out.println("ya termino el camino A");
	}
}
